package cn.edu.hunau.controller;

import cn.edu.hunau.exception.RepwdOrPwdErrorException;
import cn.edu.hunau.pojo.User;
import cn.edu.hunau.service.UserService;
import cn.edu.hunau.service.impl.UserServiceImpl;
import cn.edu.hunau.utils.JsonUtils;
import cn.edu.hunau.utils.R;
import cn.edu.hunau.utils.ResultCodeEnum;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 苏佳
 * @Date 2022 12 02 11 46
 **/
@MultipartConfig
@WebServlet("*.user")
public class UserController extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String match = req.getHttpServletMapping().getMatchValue();
        PrintWriter out = resp.getWriter();
        if("regist".matches(match)){
            String username = req.getParameter("username");
            String pwd = req.getParameter("pwd");
            String repwd = req.getParameter("repwd");

            //确认密码
            if(pwd.equals(repwd)){
                User user = userService.regist(username, pwd);
                System.out.println(user.getId());
                //跳转到登陆页面
                resp.sendRedirect("login.html");
            }else{
                //两次密码不一致
                throw new RepwdOrPwdErrorException("您输入的两次密码不一致,请重新输入!");
            }
        }
        else if ("loginByUsername".matches(match)){
            String username = req.getParameter("username");
            String pwd = req.getParameter("pwd");
            try {
                User u = userService.loginByUsername(username, pwd);

                //如果登录成功,将用户信息封装到Session中
                HttpSession session = req.getSession();
                session.setAttribute("LOGIN_USER",u);
                //重定向到首页地址
//                resp.sendRedirect("index.html");
              out.println(JsonUtils.objectToJson(R.ok().data(u)));
            } catch (Exception e) {
                out.print(JsonUtils.objectToJson(R.error().message(e.getMessage())));
            }

        }else if ("loginByTel".matches(match)){
            String tel = req.getParameter("tel");
            String pwd = req.getParameter("pwd");
            try {
                User u = userService.loginByTel(tel, pwd);

                //如果登录成功,将用户信息封装到Session中
                HttpSession session = req.getSession();
                session.setAttribute("LOGIN_USER",u);
                //重定向到首页地址
//                resp.sendRedirect("index.html");
                out.println(JsonUtils.objectToJson(R.ok().data(u)));
            } catch (Exception e) {
                out.print(JsonUtils.objectToJson(R.error().message(e.getMessage())));
            }

        } else if ("checkUsername".matches(match)) {
            //获取数据
            String name = req.getParameter("name");
            boolean flag = userService.checkUserName(name);
            if(flag){
                //用户名重复
                System.out.println("用户名重复");
                out.println(JsonUtils.objectToJson(R.setResult(ResultCodeEnum.USERNAME_HAS_EXISTS)));//丢出false

            }else{
                out.println(JsonUtils.objectToJson(R.ok().message("ok").code(200201)));
            }
        }
        else if ("updatePwd".matches(match)){
            String oldPwd = req.getParameter("oldPwd");
            String newPwd = req.getParameter("newPwd");
            String id = req.getParameter("uid");

            int uid = Integer.parseInt(id);
            boolean flag = userService.updatePwdByUid(oldPwd, newPwd, uid);

            if (flag){
                //修改成功
                out.println(JsonUtils.objectToJson(R.ok().message("修改成功")));
            }else{
                out.println(JsonUtils.objectToJson(R.error()));
            }
        }
        else if ("bandingTel".matches(match)) {
            String tel = req.getParameter("tel");
            String id = req.getParameter("uid");

            int uid = Integer.parseInt(id);
            boolean flag = userService.bandingTel(tel, uid);
            if (flag){
                //绑定成功
                out.println(JsonUtils.objectToJson(R.ok().message("绑定成功")));
            }else{
                out.println(JsonUtils.objectToJson(R.error()));
            }
        }
        else if("showUserInfor".matches(match)){
            String id = req.getParameter("uid");

            int uid = Integer.parseInt(id);
            User userInfor = userService.findMyInforByUid(uid);
            out.println(JsonUtils.objectToJson(R.ok().data(userInfor)));
        }
    }
}
